//
//  PSelectView.m
//  SHHCPhone
//
//  Created by sheng long on 14-6-18.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "PSelectView.h"

@interface PSelectView ()
@end

@implementation PSelectView

@synthesize dataArray,delegate;
@synthesize currentType;

-(void)setDataArray:(NSArray *)dArray
{
    dataArray = [[NSArray alloc] initWithArray:dArray];
    
    CGFloat x = 0;
    CGFloat y = 0;
    
    for (int i = 0 ; i < dArray.count; i++) {
        
        NSArray *array = [dArray objectAtIndex:i];
        
        UIView *view = [[UIView alloc] init];
        [view setTag:i+100];
        [self addSubview:view];
        
        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[array objectAtIndex:0]]];
        [image sizeToFit];
        [image setTag:1];
        [view addSubview:image];
        
        UILabel *label = [[UILabel alloc] init];
        [label setTag:2];
        [label setText:[array objectAtIndex:2]];
        [label setTextColor:GET_COLOR(@"ababab")];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setFont:[UIFont boldSystemFontOfSize:13]];
        [label sizeToFit];
        [view addSubview:label];
        
        [view setFrame:CGRectMake(x, 0, image.frame.size.width+5+label.frame.size.width, MAX(image.frame.size.height, label.frame.size.height))];
        [image setCenter:CGPointMake(image.frame.size.width/2, view.frame.size.height/2)];
        [label setCenter:CGPointMake(view.frame.size.width-label.frame.size.width/2, image.frame.size.height/2)];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:view.bounds];
        [view addSubview:button];
        [button addTarget:self action:@selector(selectButt:) forControlEvents:UIControlEventTouchUpInside];
        
        x = view.frame.size.width+view.frame.origin.x+5;
        y = MAX(y, view.frame.size.height);
        
    }
    self.frame = CGRectMake(0, 0, x, y);
//    [self selectIndex:0];
}

-(void)setCurrentType:(NSInteger)type
{
    [self selectIndex:type];
}

-(void)selectButt:(UIButton*)button
{
    if (currentType != button.superview.tag-100) {
        [self selectIndex:button.superview.tag-100];
    }
}

-(void)selectIndex:(NSInteger)index
{
    currentType = index;

    for (int i = 0; i < dataArray.count; i++) {
        UIView *view = [self viewWithTag:i+100];
        UIImageView *image = (UIImageView*)[view viewWithTag:1];
        UILabel *label = (UILabel*)[view viewWithTag:2];
        
        NSArray *array = [dataArray objectAtIndex:i];
        [image setImage:[UIImage imageNamed:[array objectAtIndex:0]]];
        [label setTextColor:GET_COLOR(@"ababab")];
    }
    
    UIView *view = [self viewWithTag:index+100];
    NSArray *array = [dataArray objectAtIndex:index];
    UIImageView *image = (UIImageView*)[view viewWithTag:1];
    UILabel *label = (UILabel*)[view viewWithTag:2];
    [image setImage:[UIImage imageNamed:[array objectAtIndex:1]]];
    [label setTextColor:GET_COLOR(@"febad1")];
    
    if ([(NSObject*)self.delegate respondsToSelector:@selector(selectTag:)]) {
        [self.delegate selectTag:index];
    }
}

@end
